/***************************************************************************
 *   Copyright (C) 2008/2009 by Philipp Müller                             *
 *   philipp.mueller@gmx.de                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

#include <QtCore>
#include <QKeyEvent>

#include "widgetchannellist.h"
#include "qtvomp.h"
#include "dlgchannelinformation.h"
// #include "dlgsettimer.h"
#include "channellistdelegate.h"
#include "enterchannelnumber.h"

WidgetChannelList::WidgetChannelList( MainWindow* mainWindow, QWidget *parent ) : QWidget( parent ),
    m_mainWindow( mainWindow )
{
    m_ui.setupUi( this );

    //Channel list
    ChannelListDelegate *delegate = new ChannelListDelegate();
    m_ui.channelListView->setItemDelegate( delegate );

    m_ui.channelListView->setModel( m_mainWindow->channelListModel() );

    m_ui.channelListView->setSelectionModel( m_mainWindow->channelListSelectionModel() );
    m_ui.channelListView->setSelectionMode( QAbstractItemView::SingleSelection );
    m_ui.channelListView->setSelectionBehavior( QAbstractItemView::SelectRows );
    m_ui.channelListView->setUniformItemSizes( true );

    //Play
    m_ui.playButton->setIcon( style()->standardIcon( QStyle::SP_MediaPlay ) );
    connect( m_ui.playButton, SIGNAL( clicked() ),
             this,            SLOT( playTVClicked() ) );

    //Channel information
    m_ui.informationButton->setIcon( style()->standardIcon( QStyle::SP_MessageBoxInformation ) );
    connect( m_ui.informationButton, SIGNAL( clicked() ),
             this,                   SLOT( displayChannelsProgramInfo() ) );
    //Back
    m_ui.backButton->setIcon( style()->standardIcon( QStyle::SP_ArrowLeft ) );
    connect( m_ui.backButton, SIGNAL( clicked() ),
             m_mainWindow,    SLOT( displayMenu() ) );

    connect( m_ui.epgButton, SIGNAL( clicked() ),
             m_mainWindow,    SLOT( displayEpg() ) );
}



void WidgetChannelList::setFocusOnChannelList()
{
    qDebug() << "WidgetChannelList::setFocusOnChannelList: was called";

    m_ui.channelListView->setFocus();
}


void WidgetChannelList::updateTime()
{
    m_mainWindow->channelListModel()->epgDataChanged( -1 );
    update();
}


void WidgetChannelList::playTVClicked()
{
    emit playTV();
}


void WidgetChannelList::displayChannelsProgramInfo()
{
    qDebug() << "WidgetChannelList::displayChannelsProgramInfo: was called";

    QModelIndexList selectionList = m_mainWindow->channelListSelectionModel()->selectedIndexes();
    if ( selectionList.size() != ChannelListModel::ChannelColumnsLast )
    {
        qWarning() << "WidgetChannelList::displayChannelsProgramInfo: Wrong selection list size: " <<
                      selectionList.size();
        return;
    }

    DlgChannelInformation dlg( m_mainWindow, 0, this );
    dlg.exec();

    m_ui.channelListView->setFocus();
}


void WidgetChannelList::keyPressEvent( QKeyEvent * event )
{
    switch ( event->key() )
    {

        case Qt::Key_0:
        case Qt::Key_1:
        case Qt::Key_2:
        case Qt::Key_3:
        case Qt::Key_4:
        case Qt::Key_5:
        case Qt::Key_6:
        case Qt::Key_7:
        case Qt::Key_8:
        case Qt::Key_9:
        {
            EnterChannelNumber *channel = new EnterChannelNumber( event->text(), m_mainWindow, this );
            channel->exec();
            delete channel;

            event->accept();

            return;
        }

        default:
        {
            QWidget::keyPressEvent( event );
            return;
        }
   }
}
